package com.zzu.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzu.R;
import com.zzu.adapter.OneAdapter;
import com.zzu.adapter.TwoAdapter;
import com.zzu.model.bean.PackageItem;
import com.zzu.presenter.impl.PackagePresenterImpl;
import com.zzu.utils.Constants;
import com.zzu.view.view.PackageQueryView;

import java.util.List;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-07 23:35
 * 转运任务Fragment
 */
public class FragmentTwo extends BaseFragment implements PackageQueryView<List<PackageItem>> {

    private final String TAG = FragmentTwo.class.getSimpleName();

    TwoAdapter adapter = null;
    //当没有快递信息是的显示布局
    LinearLayout ll_item_empty;
    //显示揽收信息的XRecyclerView
    XRecyclerView two_xrecyclerview;

    private int refreshType = REFRESH_TYPE_DEFAULT;

    /**
     * 首次进入默认刷新
     **/
    private static final int REFRESH_TYPE_DEFAULT = 0x01;
    /**
     * 下拉刷新
     **/
    private static final int REFRESH_TYPE_PULL = 0x03;
    /**
     * 上拉加载更多
     **/
    private static final int REFRESH_TYPE_LOAD_MORE = 0x04;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View initView() {
        Log.e(TAG, "转运任务视图被初始化了");
        View view = View.inflate(mContext, R.layout.fragment_two, null);
//        adapter = new TwoAdapter(mContext);
        ll_item_empty = view.findViewById(R.id.ll_item_empty);
        //没有包裹时显示的文字
        TextView tv_item_empty = view.findViewById(R.id.tv_item_empty);
        tv_item_empty.setText(Constants.FRAGMENT_EMPTY_TWO);
        two_xrecyclerview = view.findViewById(R.id.two_xrecyclerview);


//        two_xrecyclerview.setAdapter(adapter);

        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        two_xrecyclerview.setLayoutManager(layoutManager);

        two_xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //上拉刷新
                refreshType = REFRESH_TYPE_PULL;
                LoadData();
            }

            @Override
            public void onLoadMore() {
                //下拉刷新
                refreshType = REFRESH_TYPE_LOAD_MORE;
                LoadData();
            }
        });
        return view;
    }


    @Override
    public void initData() {
        super.initData();
        LoadData();
        Log.e(TAG, "转运任务数据被初始化了");
    }

    @Override
    public void getDates(List<PackageItem> datas) {
        if(refreshType == REFRESH_TYPE_PULL){//下拉刷新
            two_xrecyclerview.refreshComplete();//刷新完成
        }else if(refreshType == REFRESH_TYPE_LOAD_MORE){//下拉加载更多
            two_xrecyclerview.loadMoreComplete();
        }

        ll_item_empty.setVisibility(View.GONE);
        TwoAdapter adapter = new TwoAdapter(mContext,datas);
        two_xrecyclerview.setAdapter(adapter);
        //设置布局管理器
        two_xrecyclerview.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));

        if(datas == null || datas.size() == 0){
            showEmptyFragment();
            return;
        }

    }

    private void showEmptyFragment() {
        ll_item_empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void getNoDate() {
        ll_item_empty.setVisibility(View.GONE);
        Toast.makeText(getContext(),"不好意思，服务器正在维护中，请稍后重试",Toast.LENGTH_SHORT).show();
    }

    //加载数据,向适配器中添加数据
    private void LoadData() {
        PackagePresenterImpl packagePresenter = new PackagePresenterImpl(mContext,this);
        packagePresenter.getPackagesInTransit();//获取到数据进行回调getDates(List<Package> dates)
    }

}
